<?php
namespace app\home\controller;

use app\common\model\Member as MemberModel;
use app\common\model\News as NewsModel;
use app\common\model\NewsComment as NewsCommentModel;
use app\common\model\NewsCommentLike as NewsCommentLikeModel;
use app\common\model\NewsCommentMessage as NewsCommentMessageModel;
use think\Db;
/**
 * 资讯控制器
 * News Classes
 * @package app\index\controller
 */
class Newscomment extends Base
{
    /**
     * [index  一级评论列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new NewsCommentModel;
        $lmodel = new NewsCommentLikeModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            if(!empty($param['mtype'])){
                $map['c.mtype'] = $param['mtype'];
            }else{
                $map['c.mtype'] = 1;
            }
            $map['c.nid'] = $param['id'];
            $map['c.parid'] = 0;
            $map['c.status'] = 1;
            $counts = $model->getCount1($map);
            $allpage = intval(ceil($counts / $this->size));//总页面
            $lists = $model->getlist1($map, $this->from, $this->size,'m.id mid,m.img,m.username,c.id,c.content,c.create_time,c.imgs');
            foreach($lists as &$v){
                $v['content'] = urldecode($v['content']);
                $v['addtime'] = get_last_time(strtotime($v['create_time']));
                $count = $model->getAllCount(['parid'=>$v['id'],'status'=>1]);
                $v['comment_count'] = $count;
                $v['comment_like_count'] = $lmodel->getAllCount(['cid'=>$v['id'],'status'=>1]);
                $like = $lmodel->findOne(['mid'=>$user['id'],'status'=>1,'cid'=>$v['id']]);
                if(empty($like)){
                    $v['is_like'] = 0;
                }else{
                    $v['is_like'] = 1;
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $counts,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [add  添加评论]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new NewsCommentModel;
        $nmodel = new NewsCommentMessageModel;
        $user = $this->user;
        try{
            $param['content'] = urlencode($param['content']);
            if(!empty($param['to_content'])){
                $param['to_content'] = urlencode($param['to_content']);
            }
            if(!empty($param['mid'])){
                $param['comment_uid'] = $user['id'];
            }else{
                $param['mid'] = $user['id'];
            }
            $id = $model->addData($param);
            if(!empty($param['comment_uid'])){
                $nmodel->addData(['mid'=>$param['mid'],'cid'=>$id,'type'=>1]);
            }
            add_quality($user['id'],27,$user['quality'],4);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new NewsCommentModel;
        try{
            $param['status'] = 2;
            $model->editData($param);
            $comment = $model->findData($param['id'],'parid');
            if(!empty($comment)){
                Db::name('news_comment')->where(['parid'=>$param['id']])->update(['status'=>2]);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '删除成功');
    }
    /**
     * [reply  获取评论回复]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function reply()
    {
        $param = input('param.');
        $model = new NewsCommentModel;
        $lmodel = new NewsCommentLikeModel;
        $mmodel = new MemberModel;
        $nmodel = new NewsModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            $comment = $model->findData($param['id'],'id','id,mid,content,imgs,nid,create_time');
            $news = $nmodel->findData($comment['nid'],'id','title,cover');
            $comment['title'] = $news['title'];
            $comment['cover'] = $news['cover'];
            $comment['addtime'] = get_last_time(strtotime($comment['create_time']));
            $member = $mmodel->findData($comment['mid'],'id','img,username');
            $comment['img'] = $member['img'];
            $comment['username'] = $member['username'];
            $count = $model->getAllCount(['parid'=>$comment['id'],'status'=>1]);
            $comment['comment_count'] = $count;
            $comment['comment_like_count'] = $lmodel->getAllCount(['cid'=>$comment['id'],'status'=>1]);
            $like = $lmodel->findOne(['mid'=>$user['id'],'status'=>1,'cid'=>$comment['id']]);
            if(empty($like)){
                $comment['is_like'] = 0;
            }else{
                $comment['is_like'] = 1;
            }
            $comment['content'] = urldecode($comment['content']);
//            if(!empty($param['c.mtype'])){
//                $map['c.mtype'] = $param['c.mtype'];
//            }else{
//                $map['c.mtype'] = 1;
//            }
            $map['c.parid'] = $param['id'];
            $map['c.status'] = 1;
            $counts = $model->getCount1($map);
            $allpage = intval(ceil($count / $this->size));//总页面
            $lists = $model->getlist1($map, $this->from, $this->size,'m.id mid,c.comment_uid,c.id,c.content,c.create_time,c.imgs,c.to,c.to_content');
            foreach($lists as &$v){
                $member = $mmodel->findData($v['comment_uid'],'id','img,username');
                $v['img'] = $member['img'];
                $v['username'] = $member['username'];
                $v['content'] = urldecode($v['content']);
                $v['to_content'] = urldecode($v['to_content']);
                $v['addtime'] = get_last_time(strtotime($v['create_time']));
                $count = $model->getAllCount(['parid'=>$v['id'],'status'=>1]);
                $v['comment_count'] = $count;
                $v['comment_like_count'] = $lmodel->getAllCount(['cid'=>$v['id'],'status'=>1]);
                $like = $lmodel->findOne(['mid'=>$user['id'],'status'=>1,'cid'=>$v['id']]);
                if(empty($like)){
                    $v['is_like'] = 0;
                }else{
                    $v['is_like'] = 1;
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'comment'=> $comment,
            'count' => $counts,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
}